//
//  LCZMovieCollectionCell.m
//  PersonalNews
//
//  Created by Golibyo on 15/11/29.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZMovieCollectionCell.h"
#import "LCZMovieTableCell.h"
#import "LCZMovieTopCollectionCell.h"

@interface LCZMovieCollectionCell ()


//记录button点击后tableView的offset
@property(nonatomic, assign)CGFloat tempLocation;
//被点击button的cell的tag值
@property(nonatomic, assign)NSInteger tagTemp;
//点击cell的位置
@property(nonatomic, assign)CGRect rectInSuperView;
@property(nonatomic, assign)LCZMovieTableCell *cell;

@end

@implementation LCZMovieCollectionCell

- (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"456" object:nil];
}



- (void)setMovieCollectionArr:(NSMutableArray *)movieCollectionArr {
    if (_movieCollectionArr != movieCollectionArr) {
        _movieCollectionArr = movieCollectionArr;
    }
    [self.tableView reloadData];
    [self.collectionView reloadData];
}

- (void)setMovieTableArr:(NSMutableArray *)movieTableArr {
    if (_movieTableArr != movieTableArr) {
        _movieTableArr = movieTableArr;
    }
    [self.tableView reloadData];
    [self.collectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[LCZBaseTableView alloc] initWithFrame:CGRectMake(0, 20, 375, 605) style:UITableViewStylePlain];
        [self addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.rowHeight = 250;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = 1;
        flowLayout.itemSize = CGSizeMake(WIDTH / 4.0, WIDTH / 667.0 * HEIGHT / 4.0);
        self.collectionView = [[LCZBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 4.0 / 667.0 * HEIGHT) collectionViewLayout:flowLayout];
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[LCZMovieTopCollectionCell class] forCellWithReuseIdentifier:@"collectionViewReuse"];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.tableView.tableHeaderView = self.collectionView;
//        播放视频小窗口的创建
        [LCZSingleton shareSingleton].SmallMovieWindow = [[LCZBaseView alloc] initWithFrame:CGRectMake(0, HEIGHT / 667.0 * 50, 175 / 375.0 * WIDTH, 150 / 667.0 * HEIGHT)];
        [LCZSingleton shareSingleton].SmallMovieWindow.backgroundColor = [UIColor clearColor];
        [self addSubview:[LCZSingleton shareSingleton].SmallMovieWindow];
        [LCZSingleton shareSingleton].SmallMovieWindow.hidden = YES;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice) name:@"456" object:nil];
//        [LCZSingleton shareSingleton].AVPlayerView = [[LCZAVPlayer alloc] initWithFrame:CGRectMake(0, 0, 375, 250)];
//        
//        [self addSubview:[LCZSingleton shareSingleton].AVPlayerView];
//        [[LCZSingleton shareSingleton].AVPlayerView setUrl:@"http://audio.m.126.net/201507/04/011b2ababfcaf43017d3a964ce580b7c.mp3"];
//        [[LCZSingleton shareSingleton].AVPlayerView.player play];
        
    }
    return self;
}


- (void)notice {
   
    if ([LCZSingleton shareSingleton].isReturnClick) {
        if ([LCZSingleton shareSingleton].SmallMovieWindow.hidden == YES) {
            [LCZAVPlayer shareSingleton].frame = self.cell.myImageView.frame;
            [[LCZAVPlayer shareSingleton] changeFrame:self.cell.myImageView.frame];
            [self.cell.backView addSubview:[LCZAVPlayer shareSingleton]];

        } else {
            [LCZAVPlayer shareSingleton].frame = self.cell.myImageView.frame;
            [[LCZAVPlayer shareSingleton] changeFrame:self.cell.myImageView.frame];
            [[LCZSingleton shareSingleton].SmallMovieWindow addSubview:[LCZAVPlayer shareSingleton]];
        }
        
        
            }
    
    [LCZSingleton shareSingleton].isReturnClick = NO;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCZMovieTopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewReuse" forIndexPath:indexPath];
    cell.label.text = self.movieCollectionArr[indexPath.row][@"title"];
    [cell.MyImageView sd_setImageWithURL:[NSURL URLWithString:self.movieCollectionArr[indexPath.row][@"imgsrc"]]];
    return cell;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.movieTableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *reuse = [NSString stringWithFormat:@"reuse%ld", indexPath.row];
    LCZMovieTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[LCZMovieTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.firstLabel.text = self.movieTableArr[indexPath.row][@"title"];
    cell.secLabel.text = self.movieTableArr[indexPath.row][@"description"];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.movieTableArr[indexPath.row][@"cover"]]];
    cell.button.tag = 1000 + indexPath.row;
    [cell.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)click:(UIButton *)button {
//    点击后把小窗口隐藏,记录当前tableView的偏移量,点击状态设为yes
    [LCZSingleton shareSingleton].SmallMovieWindow.hidden = YES;
    self.tempLocation = self.tableView.contentOffset.y;
    [LCZSingleton shareSingleton].isClick = YES;
        self.tagTemp = button.tag - 1000;
    
//    找到button的cell
    self.cell = (LCZMovieTableCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(button.tag - 1000) inSection:0]];
    NSLog(@"%@", self.cell.firstLabel.text);
    [[LCZAVPlayer shareSingleton].player pause];
    [[LCZAVPlayer shareSingleton] removeFromSuperview];
    [[LCZAVPlayer shareSingleton] makeUrl:self.movieTableArr[button.tag - 1000][@"mp4_url"]];
    [LCZAVPlayer shareSingleton].frame = self.cell.myImageView.frame;
    [[LCZAVPlayer shareSingleton] changeFrame:self.cell.myImageView.frame];
    [self.cell.backView addSubview:[LCZAVPlayer shareSingleton]];
    NSLog(@"%@", self.movieTableArr[button.tag - 1000][@"mp4_url"]);
    [[LCZAVPlayer shareSingleton].player play];
    [[LCZAVPlayer shareSingleton].startButton addTarget:self action:@selector(fullScreenClick:) forControlEvents:UIControlEventTouchUpInside];
    //   找到cell在view上的位置
    CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:(button.tag - 1000) inSection:0]];
    self.rectInSuperView = [self.tableView convertRect:rectInTableView toView:[self.tableView superview]];
}

- (void)fullScreenClick:(UIButton *)button {
    [LCZSingleton shareSingleton].willFullScreenAppear = YES;
    [self.delegate pushView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([LCZSingleton shareSingleton].isClick) {
        //        判断点击的cell是否消失在视野中,如果消失吧单例视频对象放在haha上 出现就把这个对象放在点击的button的cell上
        if (self.tableView.contentOffset.y - self.tempLocation > (self.rectInSuperView.origin.y + self.rectInSuperView.size.height) || (self.tempLocation - self.tableView.contentOffset.y) > (self.rectInSuperView.origin.y + self.rectInSuperView.size.height)) {
            [LCZSingleton shareSingleton].SmallMovieWindow.hidden = NO;
            [[LCZAVPlayer shareSingleton] removeFromSuperview];
            [LCZAVPlayer shareSingleton].frame = self.cell.myImageView.frame;
            [[LCZAVPlayer shareSingleton] changeFrame:self.cell.myImageView.frame];
            [[LCZSingleton shareSingleton].SmallMovieWindow addSubview:[LCZAVPlayer shareSingleton]];
        } else {
            [LCZSingleton shareSingleton].SmallMovieWindow.hidden = YES;
//            LCZMovieTableCell *cell = (LCZMovieTableCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.tagTemp inSection:0]];
            [[LCZAVPlayer shareSingleton] removeFromSuperview];
            [LCZAVPlayer shareSingleton].frame = self.cell.myImageView.frame;
            [[LCZAVPlayer shareSingleton] changeFrame:self.cell.myImageView.frame];
            [self.cell addSubview:[LCZAVPlayer shareSingleton]];
        }
        
    }

}
















@end
